package com.ruoyi.reunion.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.reunion.domain.Notification;
import com.ruoyi.reunion.service.INotificationService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.reunion.domain.Reservation;
import com.ruoyi.reunion.service.IReservationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 预约Controller
 * 
 * @author ccccc
 * @date 2025-04-09
 */
@RestController
@RequestMapping("/reunion/reservation")
public class ReservationController extends BaseController
{
    @Autowired
    private IReservationService reservationService;


    @Autowired
    private INotificationService notificationService;

    //根据用户id和场地id查询预约信息
    @GetMapping("/getReservationByUserIdAndVenueId/{venueId}")
    public AjaxResult getReservationByUserIdAndVenueId(@PathVariable Long venueId) {
        Reservation reservation = new Reservation();
        reservation.setUserId(getUserId());
        reservation.setVenueId(venueId);
//        reservation.setStatus(1L);
        List<Reservation> reservations = reservationService.selectReservationList(reservation);
        return success(reservations);
    }


    //查询具体场地的预约时间段
    @GetMapping("/getReservationByVenueId/{venueId}")
    public AjaxResult getReservationByVenueId(@PathVariable Long venueId) {
        Reservation reservation = new Reservation();
        reservation.setVenueId(venueId);
        reservation.setStatus(1L);
        List<Reservation> reservations = reservationService.selectReservationList(reservation);
        //拆解每个时间段  生成日期数组
        ArrayList<String> dateList = new ArrayList<>();

        for (Reservation reservation1 : reservations) {
            //
            Date startTime = reservation1.getStartTime();
            Date endTime = reservation1.getEndTime();
            //遍历 startTime-endTime 将时间段拆解为日期数组
            while (startTime.before(endTime)) {
                //格式化 YYYY-MM-dd格式
                String date = DateUtils.dateTime(startTime);

                if (!dateList.contains(date)) {
                    dateList.add(date);
                }
                startTime = DateUtils.addDays(startTime, 1);
            }

        }
        return AjaxResult.success(dateList);
    }


    //添加预约信息
    @PostMapping("/addReservation")
    public AjaxResult addReservation(@RequestBody Reservation reservation) {
        //
        reservation.setUserId(getUserId());
        reservation.setStatus(0L);
        reservation.setCreateTime(new Date());


        Notification notification = new Notification();
        notification.setType(2L);
        notification.setContent("您已提交预约信息，请等待审核");
        notification.setTargetId(getUserId());
        notification.setTitle("预约审核通知");
        notification.setCreateTime(new Date());
        notificationService.insertNotification(notification);


        //发送预约信息

        return toAjax(reservationService.insertReservation(reservation));
    }
    @GetMapping("/ulist")
    public AjaxResult ulist(Reservation reservation)
    {
        reservation.setUserId(getUserId());
        List<Reservation> list = reservationService.selectReservationList(reservation);
        return AjaxResult.success(list);
    }

    /**
     * 查询预约列表
     */
    @PreAuthorize("@ss.hasPermi('reunion:reservation:list')")
    @GetMapping("/list")
    public TableDataInfo list(Reservation reservation)
    {
        startPage();
        List<Reservation> list = reservationService.selectReservationList(reservation);
        return getDataTable(list);
    }

    /**
     * 导出预约列表
     */
    @PreAuthorize("@ss.hasPermi('reunion:reservation:export')")
    @Log(title = "预约", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Reservation reservation)
    {
        List<Reservation> list = reservationService.selectReservationList(reservation);
        ExcelUtil<Reservation> util = new ExcelUtil<Reservation>(Reservation.class);
        util.exportExcel(response, list, "预约数据");
    }

    /**
     * 获取预约详细信息
     */
    @PreAuthorize("@ss.hasPermi('reunion:reservation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(reservationService.selectReservationById(id));
    }

    /**
     * 新增预约
     */
//    @PreAuthorize("@ss.hasPermi('reunion:reservation:add')")
    @Log(title = "预约", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Reservation reservation)
    {
        return toAjax(reservationService.insertReservation(reservation));
    }

    @PutMapping("/agreeReservation")
    public AjaxResult agreeReservation(@RequestBody Reservation reservation)
    {

        Reservation reservation1 = reservationService.selectReservationById(reservation.getId());

        Notification notification = new Notification();
        notification.setType(2L);
        notification.setTargetId(reservation1.getUserId());
        notification.setContent("您通过预约申请");
        notification.setTitle("预约通过通知");
        notification.setCreateTime(new Date());
        notificationService.insertNotification(notification);
        return toAjax(reservationService.updateReservation(reservation));
    }

    @PutMapping("/uedit")
    public AjaxResult uedit(@RequestBody Reservation reservation)
    {
        return toAjax(reservationService.updateReservation(reservation));
    }
    /**
     * 修改预约
     */
    @PreAuthorize("@ss.hasPermi('reunion:reservation:edit')")
    @Log(title = "预约", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Reservation reservation)
    {
        return toAjax(reservationService.updateReservation(reservation));
    }

    /**
     * 删除预约
     */
    @PreAuthorize("@ss.hasPermi('reunion:reservation:remove')")
    @Log(title = "预约", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(reservationService.deleteReservationByIds(ids));
    }
}
